<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="品牌名称" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货号" prop="atricleNumber">
        <el-input
          v-model="queryParams.atricleNumber"
          placeholder="请输入货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择处理状态" clearable>
          <el-option
            v-for="dict in dict.type.tb_flaw_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:flaw:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:flaw:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:flaw:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:flaw:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flawList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="品牌名称" align="center" prop="brand" />
      <el-table-column label="货号" align="center" prop="atricleNumber" />
      <el-table-column label="码数" align="center" prop="size" />
      <el-table-column label="处理状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_flaw_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="处理价格" align="center" prop="price" />
      <el-table-column label="图片" align="center" prop="picture" width="100" >
        <template slot-scope="scope">
          <el-image :src="scope.row.picture" lazy />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:flaw:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:flaw:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改瑕疵对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌" prop="brand">
          <el-select v-model="form.brand" @click.native="getBrandList" size="small" placeholder="请输入品牌" @change="changeBrand">
            <el-option v-for="brand in BrandIdList" :key="brand.id" :label="brand.brand"
            :value="brand.id">
          </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货号" prop="articleId">
          <el-select v-model="form.articleId" @click.native="getListArticleList" size="small" placeholder="请输入货号" @change="changeArticle">
            <el-option v-for="article in articleIdList" :key="article.id" :label="article.articleNumber"
            :value="article.id">
          </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="码数" prop="size">
          <el-select v-model="form.size" @click.native="getArticleList" size="small" placeholder="请输入码数" >
            <el-option v-for="article in articleIdList" :key="article.id" :label="article.size"
            :value="article.size">
          </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择处理状态">
            <el-option
              v-for="dict in dict.type.tb_flaw_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <div  v-if="form.state === '20' ">
          <el-form-item label="处理价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入处理价格" />
          </el-form-item>
        </div>
        <el-form-item label="图片" prop="picture">
          <userAvatar @uploadImage="uploadImage" :user="user" v-if="open"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlaw, getFlaw, delFlaw, addFlaw, updateFlaw } from "@/api/system/flaw";
import { listBrand } from  "@/api/system/brand";
import { listAtricle,listAtriclelist } from  "@/api/system/atricle";
import userAvatar from "../atricle/userAvatar.vue";

export default {
  name: "Flaw",
  dicts: ['tb_flaw_state'],
  components: { userAvatar },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 瑕疵表格数据
      flawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 添加弹窗品牌数据弹窗
      BrandIdList: [],
      // 货号列表
      articleIdList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleId: null,
        brand: null,
        atricleNumber: null,
        picture: null,
        state: null,
        price: null,
        createId: null,
        updateId: null,
      },
      paramsByAtricle: {
        brandId: null,
        articleNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        brand: [
          { required: true, message: "品牌名称不能为空", trigger: "blur" }
        ],
        atricleNumber: [
          { required: true, message: "货号不能为空", trigger: "blur" }
        ]
        // picture: [
        //   { required: true, message: "图片地址不能为空", trigger: "blur" }
        // ]
      },
       // 图片裁切组件props
       user: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 接受图片裁切组件返回的图片链接地址保存到表单里
    uploadImage(img){
      this.form.picture = img;
    },
    /** 查询瑕疵列表 */
    getList() {
      this.loading = true;
      listFlaw(this.queryParams).then(response => {
        this.flawList = response.data.records;
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        articleId: null,
        brand: null,
        atricleNumber: null,
        picture: null,
        state: null,
        price: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        createId: null,
        updateId: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 查询品牌列表 */
    getBrandList() {
      this.loading = true;
      listBrand(this.params).then(response => {
        this.BrandIdList = response.data.records;
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    /** 查询货号列表 */
    getArticleList() {
      this.loading = true;
      listAtricle(this.paramsByAtricle).then(response => {
        this.articleIdList = response.data.records;
        this.loading = false;
      });
    },
    getListArticleList() {
      this.loading = true;
      listAtriclelist(this.paramsByAtricle).then(response => {
        this.articleIdList = response.data;
        this.loading = false;
      });
    },
     /** change保存品牌 */
     changeBrand(val){
      var obj = {};
      obj = this.BrandIdList.find(function(item){
        if(item.id === val){
          return item;
        }
      })
      this.form.brand = obj.brand;
      this.paramsByAtricle.brandId = val
    },
    /** 写入货号 */
    changeArticle(val){
      var obj = {};
      obj = this.articleIdList.find(function(item){
        if(item.id === val){
          return item;
        }
      })
      this.paramsByAtricle.articleNumber = obj.articleNumber;
      this.form.atricleNumber=obj.articleNumber;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加瑕疵";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFlaw(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改瑕疵";
        this.user.img = this.form.picture;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFlaw(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlaw(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除瑕疵编号为"' + ids + '"的数据项？').then(function() {
        return delFlaw(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/flaw/export', {
        ...this.queryParams
      }, `flaw_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
